Modifying a Form Field on the onchange Event

Description

One of the most common requirements that developers have is to examine a field value and then, if certain requirements are met, change its value before it gets written. Many such operations are possible with field rules, but sometimes an Xbasic script is more flexible.

Example

In this case the developer wanted to change the Title field's value if it started with "The ". He wanted to remove "The " and append it to the end of the field. This was easily achieved by adding this code to the OnDepart event of the Title field.

  1. First put the form in Design Mode by clicking the following.

    images/Design_Mode_button.gif
  2. Right click the field and select Events > onchange.

  3. Paste the code and save the results.

    dim name as C
    name = title.text
    if (left(name, 4) = "The ") then
        title.text = right(name, len(name) - 4) + ", The"
    end if
    title.refresh()

What the code does

  1. First, the name variable captures the value of the Title using it's text property.

  2. Then it uses the LEFT() function to test if the first four letters are equal to "The ".

  3. If the word is found, it uses the LEN() function to find the length of name.

  4. It subtracts four characters and uses the resulting number to extract the trimmed version of the Title field using the RIGHT() function.

  5. Next, it appends ", The" to the end of the trimmed field.

  6. Then, it places the value back into title.text.

  7. Finally, it refreshes the form display, so you can see the results.